package com.simba.server.components;

import java.util.HashMap;
import org.apache.log4j.Logger;
import com.simba.common.log.LogUtil;
import com.simba.server.components.data.SlotInfo;
import com.simba.server.components.data.SortInfo;
import com.simba.server.controllers.IMachineStatusController;
import com.simba.server.controllers.IOBRStatusController;
import com.simba.server.controllers.IPacketOffController;
import com.simba.server.controllers.IPacketOnController;
import com.simba.server.controllers.ISlotPackController;
import com.simba.server.controllers.ISlotUnlockController;
import com.simba.server.controllers.impl.MachineStatusController;
import com.simba.server.controllers.impl.OBRStatusController;
import com.simba.server.controllers.impl.PacketOffController;
import com.simba.server.controllers.impl.PacketOnController;
import com.simba.server.controllers.impl.SlotPackController;
import com.simba.server.controllers.impl.SlotUnlockController;

public class SortControllerManager implements IMachineStatusController,
		IOBRStatusController, IPacketOnController, IPacketOffController,
		ISlotPackController, ISlotUnlockController
{
	private static class SortBusinessManagerHolder
	{
		private static final SortControllerManager INSTANCE = new SortControllerManager();
	}

	private static final Logger logger = Logger.getLogger(SortControllerManager.class);
	private static final String SORT_INFO_MODE_ONLINE = "Online-Sorter-Mode";

	public static SortControllerManager getInstance()
	{
		return SortBusinessManagerHolder.INSTANCE;
	}

	private String currentSortInfoMode;
	private final HashMap<String, IMachineStatusController> machineStatusControllers = new HashMap<>();
	private final HashMap<String, IOBRStatusController> obrStatusControllers = new HashMap<>();
	private final HashMap<String, IPacketOffController> packetOffControllers = new HashMap<String, IPacketOffController>();
	private final HashMap<String, IPacketOnController> packetOnControllers = new HashMap<String, IPacketOnController>();
	private final HashMap<String, ISlotPackController> slotPackControllers = new HashMap<String, ISlotPackController>();
	private final HashMap<String, ISlotUnlockController> slotUnlockControllers = new HashMap<String, ISlotUnlockController>();

	private SortControllerManager()
	{

	}

	public String getCurrentSortInfoMode()
	{
		return this.currentSortInfoMode;
	}

	public void init()
	{
		try
		{
			this.currentSortInfoMode = SortControllerManager.SORT_INFO_MODE_ONLINE;

			this.initMachineStatusController();
			this.initOBRStatusController();
			this.initPacketOnControllers();
			this.initPacketOffControllers();
			this.initSlotPackControllers();
			this.initSlotUnlockControllers();
		}
		catch (Exception ex)
		{
			LogUtil.LogError(SortControllerManager.logger,
					"Init Sort Business Manager Error -- Exception : " + ex);
		}
	}

	@Override
	public void onMachineStateMessage(int machineStatus)
	{
		IMachineStatusController controller = this.machineStatusControllers
				.get(this.currentSortInfoMode);
		if (controller != null)
		{
			controller.onMachineStateMessage(machineStatus);
		}
	}

	@Override
	public void onOBRStateMessage(String status)
	{
		IOBRStatusController controller = this.obrStatusControllers.get(this.currentSortInfoMode);
		if (controller != null)
		{
			controller.onOBRStateMessage(status);
		}
	}

	@Override
	public void onPacketOffMessage(SortInfo sortInfo)
	{
		IPacketOffController controller = this.packetOffControllers.get(this.currentSortInfoMode);
		if (controller != null)
		{
			controller.onPacketOffMessage(sortInfo);
		}
	}

	@Override
	public void onPacketOnMessage(SortInfo sortInfo)
	{
		IPacketOnController controller = this.packetOnControllers.get(this.currentSortInfoMode);
		if (controller != null)
		{
			controller.onPacketOnMessage(sortInfo);
		}
	}

	@Override
	public void onSlotPackMessage(SlotInfo slotInfo)
	{
		ISlotPackController controller = this.slotPackControllers.get(this.currentSortInfoMode);
		if (controller != null)
		{
			controller.onSlotPackMessage(slotInfo);
		}
	}

	@Override
	public void onSlotUnlockMessage(SlotInfo slotInfo)
	{
		ISlotUnlockController controller = this.slotUnlockControllers.get(this.currentSortInfoMode);
		if (controller != null)
		{
			controller.onSlotUnlockMessage(slotInfo);
		}
	}

	public void shutdown()
	{

	}

	private void initMachineStatusController()
	{
		this.machineStatusControllers.put(SortControllerManager.SORT_INFO_MODE_ONLINE,
				new MachineStatusController());
	}

	private void initOBRStatusController()
	{
		this.obrStatusControllers.put(SortControllerManager.SORT_INFO_MODE_ONLINE,
				new OBRStatusController());
	}

	private void initPacketOffControllers()
	{
		this.packetOffControllers.put(SortControllerManager.SORT_INFO_MODE_ONLINE,
				new PacketOffController());
	}

	private void initPacketOnControllers()
	{
		this.packetOnControllers.put(SortControllerManager.SORT_INFO_MODE_ONLINE,
				new PacketOnController());
	}

	private void initSlotPackControllers()
	{
		this.slotPackControllers.put(SortControllerManager.SORT_INFO_MODE_ONLINE,
				new SlotPackController());
	}

	private void initSlotUnlockControllers()
	{
		this.slotUnlockControllers.put(SortControllerManager.SORT_INFO_MODE_ONLINE,
				new SlotUnlockController());
	}
}
